package com.toivan.mt.adapter;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hwangjr.rxbus.RxBus;
import com.toivan.mt.R;
import com.toivan.mt.model.MtFaceShape;
import com.toivan.mt.model.RxBusAction;

import java.util.List;

public class MtFaceShapeAdapter extends RecyclerView.Adapter<MtDesViewHolder> {

    public static final int TYPE_COMMON = 1;
    public static final int TYPE_CRITICAL = 2;

    private final List<MtFaceShape> list;

    private int selectedPosition = 0;

    public MtFaceShapeAdapter(List<MtFaceShape> list) {
        this.list = list;
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();
    }

    @NonNull
    @Override
    public MtDesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View itemView;
        if (i == TYPE_CRITICAL) {
            itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mt_critical_point, parent, false);
        } else {
            itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mt_des, parent, false);
        }
        return new MtDesViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final MtDesViewHolder holder, int position) {
        if (position == 0) {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) holder.itemView.getLayoutParams();
            p.setMargins((int) (holder.itemView.getContext().getResources().getDisplayMetrics().density * 20 + 0.5f), 0, 0, 0);
            holder.itemView.requestLayout();
        } else {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) holder.itemView.getLayoutParams();
            p.setMargins(0, 0, 0, 0);
            holder.itemView.requestLayout();
        }
        holder.textTV.setText(list.get(position).getString(holder.itemView.getContext()));
        holder.imageIV.setImageDrawable(list.get(position).getImageDrawable(holder.itemView.getContext()));

        if (selectedPosition == position) {
            holder.textTV.setSelected(true);
            holder.imageIV.setSelected(true);
        } else {
            holder.textTV.setSelected(false);
            holder.imageIV.setSelected(false);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.getAdapterPosition() == RecyclerView.NO_POSITION) {
                    return;
                }

                selectedPosition = holder.getAdapterPosition();

                switch (list.get(holder.getAdapterPosition())) {
                    case EYE_ENLARGING:
                        RxBus.get().post(RxBusAction.ACTION_EYE_ENLARGING);
                        break;
                    case CHEEK_THINNING:
                        RxBus.get().post(RxBusAction.ACTION_CHEEK_THINNING);
                        break;
                    case CHEEK_NARROWING:
                        RxBus.get().post(RxBusAction.ACTION_CHEEK_NARROWING);
                        break;
                    case CHEEK_BONE_THINNING:
                        RxBus.get().post(RxBusAction.ACTION_CHEEK_BONE_THINNING);
                        break;
                    case JAW_BONE_THINNING:
                        RxBus.get().post(RxBusAction.ACTION_JAW_BONE_THINNING);
                        break;
                    case TEMPLE_ENLARGING:
                        RxBus.get().post(RxBusAction.ACTION_TEMPLE_ENLARGING);
                        break;
                    case HEAD_LESSENING:
                        RxBus.get().post(RxBusAction.ACTION_HEAD_LESSENING);
                        break;
                    case FACE_LESSENING:
                        RxBus.get().post(RxBusAction.ACTION_FACE_LESSENING);
                        break;
                    case CHIN_TRIMMING:
                        RxBus.get().post(RxBusAction.ACTION_CHIN_TRIMMING);
                        break;
                    case PHILTRUM_TRIMMING:
                        RxBus.get().post(RxBusAction.ACTION_PHILTRUM_TRIMMING);
                        break;
                    case FOREHEAD_TRIMMING:
                        RxBus.get().post(RxBusAction.ACTION_FOREHEAD_TRIMMING);
                        break;
                    case EYE_SPACING:
                        RxBus.get().post(RxBusAction.ACTION_EYE_SPACING);
                        break;
                    case EYE_CORNER_TRIMMING:
                        RxBus.get().post(RxBusAction.ACTION_EYE_CORNER_TRIMMING);
                        break;
                    case EYE_CORNER_ENLARGING:
                        RxBus.get().post(RxBusAction.ACTION_EYE_CORNER_ENLARGING);
                        break;
                    case NOSE_ENLARGING:
                        RxBus.get().post(RxBusAction.ACTION_NOSE_ENLARGING);
                        break;
                    case NOSE_THINNING:
                        RxBus.get().post(RxBusAction.ACTION_NOSE_THINNING);
                        break;
                    case NOSE_APEX_LESSENING:
                        RxBus.get().post(RxBusAction.ACTION_NOSE_APEX_LESSENING);
                        break;
                    case NOSE_ROOT_ENLARGING:
                        RxBus.get().post(RxBusAction.ACTION_NOSE_ROOT_ENLARGING);
                        break;
                    case MOUTH_TRIMMING:
                        RxBus.get().post(RxBusAction.ACTION_MOUTH_TRIMMING);
                        break;
                    case MOUTH_SMILING:
                        RxBus.get().post(RxBusAction.ACTION_MOUTH_SMILING);
                        break;
                    case DARK_CIRCLE:
                        RxBus.get().post(RxBusAction.ACTION_DARK_CIRCLES);
                        break;

                }
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }
}
